

var parMenu;
var sub1Menu;
var sub2Menu;

$(document).ready(function(){

	queryParMenu();
});

/**
 * 获取菜单
 */
function queryParMenu(){
	$.post("auth/queryAuthMenu.do",{"parentId":"00"},function(data){
		$("#menuTree").html(menuLoop(data,0));
	},"json");
}

var menuHtml='<li class=""><a data-url="page/buttons.html" href="#page/buttons.html"> '+
'<i class="menu-icon fa fa-tachometer"></i> <span class="menu-text">Dashboard </span>'+
'</a> <b class="arrow"></b></li>';
var sub1Html='';
var sub2Html='';
function menuLoop(arr,level){
	level+=1;
    for(var i = 0 ; i < arr.length ; i ++)
    {	
    	var fartherMenuHtml='';
    	var subMenuUl='<ul class="submenu">#1</ul>';
    	var subMenuLi='';
    	
		var name=arr[i].text;
		var url=arr[i].url;
		console.info("顺序:"+level);
    	if(level==1){
    		menuHtml=menuHtml.replace(new RegExp("#2","i"), sub1Html);
    		sub1Html='';
    		fartherMenuHtml='<li class=""><a href="#" class="dropdown-toggle">'+
    		'<class="menu-icon fa fa-desktop"></i> <span class="menu-text">#1</span> '+
    		'<b class="arrow fa fa-angle-down"></b></a> <b class="arrow"></b>#2</li>';
    		menuHtml+=fartherMenuHtml.replace(new RegExp("#1","i"), name);
    	}else if(level==2){
			subMenuUl=subMenuUl.replace(new RegExp("#1","i"), sub2Html);
			sub1Html=sub1Html.replace(new RegExp("#2","i"), subMenuUl);
			
    		sub2Html='';
			subMenuLi='<li class=""><a href="javascript:;" class="dropdown-toggle"> '+
			'<i class="menu-icon fa fa-caret-right"></i> #1<b class="arrow fa fa-angle-down"></b>'+
			'</a> <b class="arrow"></b>#2</li>';
			subMenuLi=subMenuLi.replace(new RegExp("#1","i"), name);
			sub1Html+=subMenuLi;
    	}else{
			subMenuLi='<li class=""><a href="#1" class="dropdown-toggle"> '+
			'<i class="menu-icon fa fa-caret-right"></i> #2<b class="arrow fa fa-angle-down"></b>'+
			'</a> <b class="arrow"></b></li>';
			subMenuLi=subMenuLi.replace(new RegExp("#1","i"), url);
			subMenuLi=subMenuLi.replace(new RegExp("#2","i"), name);
			sub2Html+=subMenuLi;
    	}
        if(arr[i].children instanceof Array&&arr[i].children.length>0) {
        	menuLoop(arr[i].children,level);
        }
    }
    return menuHtml;
}